<?php

namespace app\common\model;

use think\Model;

/**
 * Class _Model
 * @package app\common\model
 */
class _Model extends Model
{
    /**
     * @param $attributes
     * @param array $values
     * @return false|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function updateOrCreate($attributes, $values = [])
    {
        return $this->firstOrNew($attributes, $values)->save();
    }

    /**
     * @param $attributes
     * @param array $values
     * @return _Model|array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function firstOrNew($attributes, $values = [])
    {
        if (!is_null($instance = $this->where($attributes)->find())) {
            return $instance;
        }

        return new static($attributes + $values);
    }
}